﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SaiPuLSCoffee.model
{
    /// <summary>
    /// 机器人控制
    /// </summary>
    public class RobotOperate
    {
        public static string RobotOperateUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        public static int handle1 = 0;
        public static int handle2 = 0;
        private static RobotOperate instance;
        private RobotOperate() { }
        public static RobotOperate Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RobotOperate();
                }
                return instance;
            }
        }
        public JKTYPE.ProgramState pstatusL;
        public JKTYPE.RobotState stateL;
        public JKTYPE.RobotStatus statusL;
        public void CX_Start_L()
        {
            try
            {
                jakaAPI.get_program_state(ref handle1, ref pstatusL);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void CX_kj_L()
        {
            try
            {
                jakaAPI.get_robot_state(ref handle1, ref stateL);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void CX_status_L()
        {
            try
            {
                jakaAPI.get_robot_status(ref handle1, ref statusL);
            }
            catch (Exception)
            {
                return;
            }
        }
        public static void Robot_Initialisation_double()//机器人初始化
        {
            IOMsg.Instance.AllBol = false;
            Console.WriteLine(DigitalMsg.Instance.RobotPowerOn1_start);
            Thread.Sleep(300);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_start);
            Thread.Sleep(300);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn2_start);
            Thread.Sleep(300);
            IOMsg.Instance.AllBol = true;
            int tool_idset1 = 0;
            int frame_idset1 = 0;
            int result1 = -1;
            int result2 = -1;
            while (result1 == -1)
            {
                result1 = jakaAPI.create_handler(IPorPortInfo.Instance.RobotIPLeft.ToCharArray(), ref handle1);
                Thread.Sleep(100);
            }
            while (result2 == -1)
            {
                result2 = jakaAPI.create_handler(IPorPortInfo.Instance.RobotIPRight.ToCharArray(), ref handle2);
                Thread.Sleep(100);
            }
            IOMsg.Instance.AllBol = false;
            Thread.Sleep(300);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_end);
            Thread.Sleep(300);
            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn2_end);
            Thread.Sleep(300);
            IOMsg.Instance.AllBol = true;
            System.Threading.Thread.Sleep(3000); //毫秒
            //机器人上电
            jakaAPI.power_on(ref handle1); Console.WriteLine("L_Robot Power On");
            //切换当前使用的工具坐标
            jakaAPI.set_tool_id(ref handle1, tool_idset1); Console.WriteLine("L_Robot_T " + tool_idset1);
            //切换当前使用的世界坐标
            jakaAPI.set_user_frame_id(ref handle1, frame_idset1); Console.WriteLine("L_Robot_F " + frame_idset1);
            StringBuilder Version = new StringBuilder("", 3000);
            int result3 = jakaAPI.get_sdk_version(ref handle1, Version);
            Console.WriteLine("SDK 版本 " + Version);
            //IOMsg.Instance.QK();
        }
        public void Robot_L_Show()
        {
            CX_status_L();
            CX_Start_L();
            if (statusL.powered_on == 1)
                RobotMsgs(false, true, false, false, false, false, false);
            if (statusL.enabled == 1)
                RobotMsgs(false, false, true, false, false, false, false);
            if (pstatusL == JKTYPE.ProgramState.PROGRAM_RUNNING)
            {
                RobotMsgs(false, false, false, true, false, false, false);
                //if (WuLiaoCoffeeSel.Instance.RobotWarn != true)
                //{
                //    WuLiaoCoffeeSel.Instance.RobotWarn = true;
                //    ClientList clientList2 = new ClientList();
                //    clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
                //    clientList2.code = 2;
                //    SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
                //    Thread.Sleep(1000);
                //}
            }
            if (pstatusL == JKTYPE.ProgramState.PROGRAM_IDLE)
                RobotMsgs(false, false, false, false, true, false, false);
            if (pstatusL == JKTYPE.ProgramState.PROGRAM_PAUSED)
                RobotMsgs(false, false, false, false, false, true, false);
            if (statusL.enabled == 0)
                RobotMsgs(false, true, false, false, false, false, false);
            if (statusL.powered_on == 0)
                RobotMsgs(true, false, false, false, false, false, false);
            if (statusL.errcode != 0)
                RobotMsgs(false, false, false, false, false, false, true);
        }

        /// <summary>
        /// 机器人状态
        /// </summary>
        /// <param name="KJ">开机</param>
        /// <param name="SD">上电</param>
        /// <param name="SN">使能</param>
        /// <param name="YX">运行</param>
        /// <param name="TZ">停止</param>
        /// <param name="ZT">暂停</param>
        /// <param name="BJ">报警</param>
        ///  <param name="type">左右手 0 左 1 右</param>
        public void RobotMsgs(bool KJ, bool SD, bool SN, bool YX, bool TZ, bool ZT, bool BJ, int type = 0)
        {
            if (type == 0)
            {
                RobotMsg.Instance.Robot_YKJ_L = KJ;
                RobotMsg.Instance.Robot_SD_L = SD;
                RobotMsg.Instance.Robot_SN_L = SN;
                RobotMsg.Instance.Robot_YX_L = YX;
                RobotMsg.Instance.Robot_TZ_L = TZ;
                RobotMsg.Instance.Robot_ZT_L = ZT;
                RobotMsg.Instance.Robot_BJ_L = BJ;
            }
            else
            {
                RobotMsg.Instance.Robot_YKJ_R = KJ;
                RobotMsg.Instance.Robot_SD_R = SD;
                RobotMsg.Instance.Robot_SN_R = SN;
                RobotMsg.Instance.Robot_YX_R = YX;
                RobotMsg.Instance.Robot_TZ_R = TZ;
                RobotMsg.Instance.Robot_ZT_R = ZT;
                RobotMsg.Instance.Robot_BJ_R = BJ;
            }
        }
        public void Robot_Yun()
        {
            try
            {
                jakaAPI.program_run(ref handle1);
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
